package com.bluesky.plum.uimodels.standard.components.containers.form;

import java.util.ArrayList;
import java.util.List;

import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponent;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.Button;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.Label;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.form.commonForms.ParameterChangedEvent;
import com.bluesky.plum.uimodels.standard.components.containers.layout.Grid;
import com.bluesky.plum.uimodels.standard.components.containers.layout.HorizontalBox;
import com.bluesky.plum.uimodels.standard.components.containers.layout.VerticalBox;

/**
 * Form is an advanced UIComponent, based on Panel. It has events, field
 * components, command buttons and return values. But since we don't have usable
 * layout mechanism, we have to make Form a native component.
 * 
 * @author HZ00260
 * 
 */
public class Form extends UIComponent {

	/**
	 * a event after the form creating is done.
	 */
	protected EventListener createEvent;
	protected ParameterChangedEvent parameterChangedEvent;

	protected VerticalBox outerBox;
	protected Grid fieldComponentGrid;
	protected HorizontalBox commandButtonBox;

	protected Panel returnValuePanel;
	
	protected FieldUIComponent[] fieldUiComponents;
	

	@Override
	public void setUiComponentBuilder(UIComponentBuilder uiComponentBuilder) {

		super.setUiComponentBuilder(uiComponentBuilder);

		outerBox = (VerticalBox) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.VerticalBox);
		outerBox.setName("outerBox");

		fieldComponentGrid = (Grid) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Grid);
		fieldComponentGrid.setGrid(0, 4);
		fieldComponentGrid.setName("fieldComponentGrid");

		commandButtonBox = (HorizontalBox) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.HorizontalBox);
		commandButtonBox.setName("commandButtonBox");

		returnValuePanel = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);
		returnValuePanel.setName("returnValuePanel");

		
		outerBox.addChildComponent(fieldComponentGrid);
		outerBox.addChildComponent(commandButtonBox);
		outerBox.addChildComponent(returnValuePanel);

		addChildComponent(outerBox);

	}

	public void addFieldComponents(final FieldUIComponent[] fieldComponents) {
		UIComponentBuilder builder = getUiComponentBuilder();
		this.fieldUiComponents = fieldComponents;

		for (int i = 0; i < fieldComponents.length; i++) {
			final int j = i;

			Label label = (Label) builder
					.createNonFieldComponent(StandardUIComponentType.Label);

			label.setCaption(fieldComponents[i].getCaption());

			fieldComponentGrid.addChildComponent(label);
			fieldComponentGrid.addChildComponent(fieldComponents[i]);

			// add valueChangeEvent
			fieldComponents[i].setValueChangeEventListener(new EventListener() {
				@Override
				public void process() {
					if (getParameterChangedEvent() != null) {

						getParameterChangedEvent().changed(j, fieldComponents);
					}

				}
			});

		}
	}

	public void addButton(String name, EventListener eventListener) {
		UIComponentBuilder builder = getUiComponentBuilder();

		Button btn = (Button) builder
				.createNonFieldComponent(StandardUIComponentType.Button);
		btn.setCaption(name);
		btn.setClickListener(eventListener);
		commandButtonBox.addChildComponent(btn);

	}

	public ParameterChangedEvent getParameterChangedEvent() {
		return parameterChangedEvent;
	}

	public void setParameterChangedEvent(
			ParameterChangedEvent parameterChangedEvent) {
		this.parameterChangedEvent = parameterChangedEvent;
		
		

	}

	public void onCreate(){
		if(getCreateEvent()!=null)
			getCreateEvent().process();
	}


	@Override
	public Object getNativeComponent() {

		return outerBox.getNativeComponent();
	}

	public void print() {
		System.out.println(this.getClass().getSimpleName());
		for (UIComponent c : getChildrenComponents()) {
			System.out.println(c);
		}
	}

	public VerticalBox getOuterBox() {
		return outerBox;
	}

	public void setOuterBox(VerticalBox outerBox) {
		this.outerBox = outerBox;
	}

	public Grid getFieldComponentGrid() {
		return fieldComponentGrid;
	}

	public void setFieldComponentGrid(Grid fieldComponentGrid) {
		this.fieldComponentGrid = fieldComponentGrid;
	}

	public HorizontalBox getCommandButtonBox() {
		return commandButtonBox;
	}

	public void setCommandButtonBox(HorizontalBox commandButtonBox) {
		this.commandButtonBox = commandButtonBox;
	}

	public Panel getReturnValuePanel() {
		return returnValuePanel;
	}

	public void setReturnValuePanel(Panel returnValuePanel) {
		this.returnValuePanel = returnValuePanel;
	}

	public FieldUIComponent[] getFieldUiComponents() {
		return fieldUiComponents;
	}

	public void setFieldUiComponents(FieldUIComponent[] fieldUiComponents) {
		this.fieldUiComponents = fieldUiComponents;
	}

	public EventListener getCreateEvent() {
		return createEvent;
	}

	public void setCreateEvent(EventListener createEvent) {
		this.createEvent = createEvent;
	}

}
